package com.wzw.basics.concurrent.thread;

import java.util.concurrent.locks.LockSupport;
import java.util.logging.Logger;

/**
 * 线程生命周期
 */
public class LifeCycle {

    private static final Logger LOGGER = Logger.getLogger("Thread Life Cycle");

    static class MyThread extends Thread {
        @Override
        public void run() {
            LOGGER.info("Extends Thread");
            runnable();
            timedWaiting(2);
        }
    }

    /**
     * 新建
     */
    public static Thread newCycle(int op) {
        LOGGER.info("New Thread");
        switch (op) {
            // 2.实现Runnable接口
            case 1:
                return new Thread(() -> {
                    LOGGER.info("Impl Runnable");
                    runnable();
                });
            // 1.继承Thread类
            case 2:
                return new MyThread();
            default:
                throw new IllegalArgumentException();
        }
    }

    /**
     * 可运行/运行中
     */
    public static void runnable() {
        LOGGER.info(Thread.currentThread().getName() + " Thread Status is Runnable");
    }

    /**
     * 阻塞，等待锁资源
     */
    public static void blocked() {

    }

    /**
     * 等待，需要当前对象去唤醒
     */
    public static void waiting(int op) {
        LOGGER.info(Thread.currentThread().getName() + " Thread Status About To Become Waiting");
        try {
            switch (op) {
                case 1:
                    LOGGER.info(Thread.currentThread().getName() + " Object.wait()");
                    Thread.currentThread().wait();
                    break;
                case 2:
                    LOGGER.info(Thread.currentThread().getName() + " Object.join()");
                    Thread.currentThread().join();
                    break;
                case 3:
                    LOGGER.info(Thread.currentThread().getName() + " LockSupport.park()");
                    LockSupport.park();
                    break;
                default:
                    throw new IllegalArgumentException();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 超时等待，设置等待时长。带时间后会变成就绪态，抢夺CPU资源
     */
    public static void timedWaiting(int op) {
        LOGGER.info(Thread.currentThread().getName() + " Thread Status About To Become TimeWaiting");
        try {
            switch (op) {
                case 1:
                    LOGGER.info(Thread.currentThread().getName() + " Object.wait(1000)");
                    Thread.currentThread().wait(1000);
                    break;
                case 2:
                    LOGGER.info(Thread.currentThread().getName() + " Object.join(1000)");
                    Thread.currentThread().join(1000);
                    break;
                case 3:
                    LOGGER.info(Thread.currentThread().getName() + " Thread.sleep(1000)");
                    Thread.sleep(1000);
                    break;
                case 4:
                    LOGGER.info(Thread.currentThread().getName() + " LockSupport.parkNanos(1000)");
                    LockSupport.parkNanos(1000);
                    break;
                case 5:
                    LOGGER.info(Thread.currentThread().getName() + " LockSupport.parkUntil(1000)");
                    LockSupport.parkUntil(1000);
                    break;
                default:
                    throw new IllegalArgumentException();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 终止
     */
    public static void terminated(int op) {
        LOGGER.info(Thread.currentThread().getName() + " Thread is about to terminate");
        switch (op) {
            case 1:
                LOGGER.info(Thread.currentThread().getName() + " End of run");
                break;
            case 2:
                LOGGER.info(Thread.currentThread().getName() + " Abnormal exit");
                throw new RuntimeException();
            case 3:
                throw new IllegalArgumentException();
        }
    }
}
